package com.aier.admin_new.action;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import sun.org.mozilla.javascript.internal.regexp.SubString;

import com.aier.admin_new.entity.NameingBean;
import com.aier.admin_new.service.IClassService;
import com.aier.admin_new.service.IClassUserService;
import com.aier.classes.entity.ClassStudent;

import com.aier.common.common.SystemDateFormatUtil;
import com.aier.user.action.UserBaseAction;
import com.aier.user.entity.CallRoll;
import com.aier.user.entity.CallRollClass;
import com.aier.user.entity.CallRollSchool;
import com.google.gson.Gson;
import com.google.gson.JsonArray;

@Scope("prototype")
@Component("newCallRollAction")
public class NewCallRollAction extends AdminNewBaseAction {

	/**
	 * 点名
	 */
	private static final long serialVersionUID = 1L;
	@Resource(name = "newClassUserService")
	private IClassUserService classUserService;
	@Resource(name = "newClassService")
	private IClassService classService;

	private int s; // 错误码
	private Long cid; // 班级id
	private Integer sid; // 学校id
	private String month; // “yyyy-MM”
	private String jsonStr;
	private List<Map<String, Long>> crList; // 点名信息
	private List<CallRollClass> crcList; // 班级点名统计信息
	private List<CallRollSchool> crsList; // 学校点名统计信息
	private List<ClassStudent> csList; // 班级的全部学生
	private List<CallRoll> crl; // 某天某班级的点名情况
	private Integer isGetParent; // 是否获取学生家长
	private Integer isCall;//

	@Override
	public String execute() {
		// // 获取班级的学生列表
		// csList = classUserService.searchAllClassStudentList(cid);
		// s = 1;
		return SUCCESS;
	}

	/**
	 * 获取班级的学生列表
	 * 
	 * @return 
	 */
	public String searchCSList() {
		int i = 0;
		//是否获取家长信息
		if (isGetParent != 1) {
			//是否已经点名
			i = classUserService.isCallRoll(cid);
			//未点名
			if (i == 0) {
				csList = classUserService.searchAllClassStudentList(cid);
			} else {
				isCall = 1;
			}
		} else {
			csList = classUserService.searchAllClassStudentList(cid);
			for (ClassStudent cr : csList) {
				cr.setCpList(classUserService.searchCPByCondition(cid,
						cr.getCsid()));
			}
		}
		s = 1;
		return SUCCESS;
	}

	/**
	 * 点名
	 * 
	 * @return
	 */
	public String callRoll() {
		// 转换JSON对象
		Gson gson = new Gson();
		java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<List<NameingBean>>() {
		}.getType();
		List<NameingBean> users = gson.fromJson(jsonStr.toString(), type);

		if (users == null) {
			users = new ArrayList<NameingBean>();
		}
		// s: 1成功 0已点过名
		s = classService.insertCallRoll(cid, users);
		Timestamp cdate = new Timestamp(new Date().getTime());
		String dateStr = SystemDateFormatUtil.formatDate(cdate,
				SystemDateFormatUtil.FORMAT_DATE);
		crl = classService.searchCRByDay(cid, dateStr);
		for (CallRoll cr : crl) {
			cr.setCpList(classUserService.searchCPByCondition(cid, cr.getCsid()));
		}

		return SUCCESS;
	}

	public void setJsonStr(String jsonStr) {
		this.jsonStr = jsonStr;
	}

	/**
	 * 按天统计点名（班级教师）
	 * 
	 * @return
	 */
	public String crcByDay() {

		crcList = classService.searchCRSByDay(cid, month);

		for (CallRollClass crc : crcList) {
			if (crc.getCount() > 0) {
				List<CallRoll> crl = classService.searchCRByDay(cid,
						crc.getCdate());
				for (CallRoll cr : crl) {
					cr.setCpList(classUserService.searchCPByCondition(cid,
							cr.getCsid()));
				}
				crc.setCrList(crl);
			}

		}
		s = 1;
		return SUCCESS;
	}

	/**
	 * 本月缺勤达人榜
	 * 
	 * @return
	 */
	public String crcByMonth() {
		csList = classUserService.searchCSAbsentByMonth(cid, month);
		for (ClassStudent cs : csList) {
			cs.setCpList(classUserService.searchCPByCondition(cid, cs.getCsid()));
		}
		s = 1;
		return SUCCESS;
	}

	/**
	 * 按天统计点名（班级教师）以及本月缺勤达人榜（app端）
	 * 
	 * @return
	 */
	public String crcByDayAndMonth() {

		crcList = classService.searchCRSByDay(cid, month);

		for (CallRollClass crs : crcList) {
			List<CallRoll> crl = classService
					.searchCRByDay(cid, crs.getCdate());
			for (CallRoll cr : crl) {
				cr.setCpList(classUserService.searchCPByCondition(cid,
						cr.getCsid()));
			}
			crs.setCrList(crl);
		}

		csList = classUserService.searchCSAbsentByMonth(cid, month);
		for (ClassStudent cs : csList) {
			cs.setCpList(classUserService.searchCPByCondition(cid, cs.getCsid()));
		}
		s = 1;
		return SUCCESS;
	}

	/**
	 * 按天统计点名（所有班级，学校管理员）
	 * 
	 * @return
	 */
	public String crsByDayAllClass() {

		crsList = classService.searchCRSByDayAllClass(sid, month);

		for (CallRollSchool crs : crsList) {
			if (crs.getCount() > 0) {
				List<CallRollClass> crcl = classService.searchCRCByDayAllClass(
						sid, crs.getCdate());
				for (CallRollClass crc : crcl) {
					crc.setCrList(classService.searchCRByDay(crc.getCid(),
							crc.getCdate()));
					crc.setCtList(classUserService.searchClassAdminTeacher(crc
							.getCid()));
				}
				crs.setCrcList(crcl);
			}
		}
		s = 1;

		return SUCCESS;
	}

	/**
	 * 按月统计点名（所有班级，学校管理员）
	 * 
	 * @return
	 */
	public String crsByMonthAllClass() {
		crsList = classService.searchCRSByDayAllClass(sid, month);
		s = 1;
		return SUCCESS;
	}

	/**
	 * 查询某个学校某一天每个班级的缺勤情况 ADD by zhou 2014-5-30
	 * 
	 * @return
	 */
	public String crcByDayAllClass() {
		crcList = classService.searchCRCByMonthAllClass(sid, month);
		for (CallRollClass crc : crcList) {
			if (crc.getMcount() > 0) {
				crc.setCrList(classService.searchCRByMonth(crc.getCid(), month));
			}
		}

		s = 1;
		return SUCCESS;
	}

	public int getS() {
		return s;
	}

	public void setCid(Long cid) {
		this.cid = cid;
	}

	public void setClassUserService(IClassUserService classUserService) {
		this.classUserService = classUserService;
	}

	public List<ClassStudent> getCsList() {
		return csList;
	}

	public void setClassService(IClassService classService) {
		this.classService = classService;
	}

	public void setCrList(ArrayList<Map<String, Long>> crList) {
		this.crList = crList;
	}

	public void setMonth(String month) {
		this.month = month;
	}

	public List<CallRoll> getCrl() {
		return crl;
	}

	public void setSid(Integer sid) {
		this.sid = sid;
	}

	public List<CallRollClass> getCrcList() {
		return crcList;
	}

	public List<CallRollSchool> getCrsList() {
		return crsList;
	}

	public void setIsGetParent(Integer isGetParent) {
		this.isGetParent = isGetParent;
	}

	public Integer getIsCall() {
		return isCall;
	}

}
